// pagesDoctor/clinicalDiagnosisDeputy/index.js
import {
  getICDDataCN,
  getICDData
} from '../../api/docIndex'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    params: {
      size: 30,
      page: 1,
      finish: false,
    },
    active: '001',
    finish: false,
    loading: false,
  },

  onSearch(e) {
    this.setData({
      'params.page':1
    })
    this.getData(this.data.active, e.detail)
  },
  onLoadMore() {
    if (this.data.loading || this.data.params.finish) return
    this.data.params.page += 1
    this.getData(this.data.active)
  },

  onChangeTab(e) {
    this.data.params.page = 1
    this.setData({
      active: e.detail.name
    })
    this.getData(this.data.active)
  },
  getData(active, e) {
    let DoctorCommentListDat = []
    wx.showLoading({
      title: '加载中',
    })
    if (active == '002') {
      getICDDataCN({
        ICDName: e,
        ...this.data.params
      }).then(res => {
        wx.hideLoading()
        if (e) {
          this.setData({
            list: res.data,
            loading: false
          })
          return
        }
        const data = res.data
        DoctorCommentListDat = this.data.params.page === 1 ? data : this.data.list.concat(data)
        this.data.params.finish = DoctorCommentListDat.length >= res.page.total
        this.setData({
          list: DoctorCommentListDat,
          loading: false
        })
      })
    } else {
      getICDData({
        ICDName: e,
        ...this.data.params
      }).then(res => {
        if (e) {
          this.setData({
            list: res.data,
            loading: false
          })
          return
        }
   
        const data = res.data
        DoctorCommentListDat = this.data.params.page === 1 ? data : this.data.list.concat(data)
        this.data.params.finish = DoctorCommentListDat.length >= res.page.total
        this.setData({
          list: DoctorCommentListDat,
          loading: false
        })
      }).finally(_=>{
        wx.hideLoading()
      })

    }
  },
  onChange(e) {
    if(e.detail){
      let val = this.data.list.find(item=>{
        if(item.ICDName==e.detail){
          return item
        }
      })
      this.setData({
        name: e.detail,
        code:val.ICDCode
      })
    }
   
  },

  submitBtn(e) {
    //确认按钮进行提交
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    prevPage.setData({
      disease: this.data.name.toString(),
      disease_code:this.data.code.toString(),
      active:this.data.active
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.onChangeTab({
      detail: {
        name: '001'
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})